from flask import Blueprint, render_template, request
from .auth_manager import AuthManager  # 导入 AuthManager 类

auth_bp = Blueprint('auth', __name__)

# 实例化 AuthManager
auth_manager = AuthManager()

@auth_bp.route('/register', methods=['GET', 'POST'])
def register_page():
    if request.method == 'GET':
        return render_template('Register.html')

    username = request.form.get('username')
    password = request.form.get('password')
    phone = request.form.get('phone')
    addr = request.form.get('addr')
    user_role = request.form.get('userRole')

    # 调用 AuthManager 的注册方法
    result = auth_manager.register_user(username, password, phone, addr, user_role)
    return render_template('Register.html', messages=result)

@auth_bp.route('/logIn', methods=['GET', 'POST'])
def log_in_page():
    if request.method == 'GET':
        return render_template('logIn.html')

    username = request.form.get('username')
    password = request.form.get('password')
    user_role = request.form.get('userRole')

    # 调用 AuthManager 的登录方法
    result = auth_manager.login_user(username, password, user_role)
    return render_template('logIn.html', messages=result)